﻿using System;
using WickedFlame.Core.Shapes;
using WickedFlame.Core.State;

namespace WickedFlame.Core.History
{
    public class ConvertToPolygonCommand : HistoryCommand
    {
        VectorShape shape;
        PolygonShape polygon;

        ShapeMemento shapeState;
        ShapeMemento polygonState;

        public ConvertToPolygonCommand(VectorShape shape, PolygonShape polygon, int layerIndex)
            : base(layerIndex)
        {
            Title = "Convert to Polygon";

            this.shape = shape;
            if (shape is IStateCapturable)
                this.shapeState = ((IStateCapturable)shape).CreateMementoState();

            this.polygon = polygon;
            if (polygon is IStateCapturable)
                this.polygonState = ((IStateCapturable)polygon).CreateMementoState();
        }

        public override void Undo(ShapeList list)
        {
            list.Remove(polygon);

            if (shapeState != null)
                ((IStateCapturable)shape).ResetState(shapeState);

            list.Add(shape);
        }

        public override void Redo(ShapeList list)
        {
            list.Remove(shape);

            if (polygonState != null)
                ((IStateCapturable)polygon).ResetState(polygonState);

            list.Add(polygon);
        }
    }
}
